/*
 * Copyright (c) 2004-2022, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.test.web;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.jsontree.JsonList;
import org.hisp.dhis.jsontree.JsonObject;
import org.hisp.dhis.test.web.HttpStatus.Series;
import org.hisp.dhis.test.web.WebClient.HttpResponse;
import org.hisp.dhis.test.web.WebClient.RequestComponent;
import org.hisp.dhis.test.webapi.json.domain.JsonError;
import org.hisp.dhis.test.webapi.json.domain.JsonErrorReport;
import org.hisp.dhis.test.webapi.json.domain.JsonObjectReport;
import org.hisp.dhis.test.webapi.json.domain.JsonTypeReport;

/**
 * Helpers needed when testing with {@link WebClient} and {@code
 * org.springframework.test.web.servlet.MockMvc}.
 *
 * @author Jan Bernitt
 */
public class WebClientUtils {

  private static final Pattern IS_MEDIA_TYPE = Pattern.compile("^[-a-z]+/[-a-z]+:");

  public static String plainTextOrJson(String body) {
    if (startsWithMediaType(body)) {
      return body.substring(body.indexOf(':') + 1);
    }
    return body == null ? null : body.replace('\'', '"');
  }

  public static boolean startsWithMediaType(String body) {
    return body != null && IS_MEDIA_TYPE.matcher(body).find();
  }

  /**
   * Asserts that the {@link HttpResponse} has the expected {@link HttpStatus}.
   *
   * <p>If status is {@link HttpStatus#CREATED} the method returns the UID of the created object in
   * case it is provided by the response. This is based on a convention used in DHIS2.
   *
   * @param expected status we should get
   * @param actual the response we actually got
   * @return UID of the created object (if available) or {@code null}
   */
  public static String assertStatus(HttpStatus expected, HttpResponse actual) {
    HttpStatus actualStatus = actual.status();
    if (expected != actualStatus) {
      // OBS! we use the actual state to not fail the check in error
      JsonError error = actual.error(actualStatus.series());
      String msg = error.getMessage();
      if (msg != null && expected.series() == actualStatus.series()) {
        assertEquals(expected, actualStatus, msg);
      } else {
        assertEquals(expected, actualStatus, error.summary());
      }
    }
    assertValidLocation(actual);
    return getCreatedId(actual);
  }

  /**
   * Asserts that the {@link HttpResponse} has the expected {@link Series}. This is useful on cases
   * where it only matters that operation was {@link Series#SUCCESSFUL} or say {@link
   * Series#CLIENT_ERROR} but not which exact code of the series.
   *
   * <p>If status is {@link HttpStatus#CREATED} the method returns the UID of the created object in
   * case it is provided by the response. This is based on a convention used in DHIS2.
   *
   * @param expected status {@link Series} we should get
   * @param actual the response we actually got
   * @return UID of the created object (if available) or {@code null}
   */
  public static String assertSeries(Series expected, HttpResponse actual) {
    Series actualSeries = actual.series();
    if (expected != actualSeries) {
      // OBS! we use the actual state to not fail the check in error
      String msg = actual.error(actualSeries).summary();
      assertEquals(expected, actualSeries, msg);
    }
    assertValidLocation(actual);
    return getCreatedId(actual);
  }

  public static void assertValidLocation(HttpResponse actual) {
    String location = actual.location();
    if (location == null) {
      return;
    }
    assertTrue(
        location.startsWith("http://") || location.startsWith("https://"),
        "Location header does not start with http or https");
    assertTrue(
        location.indexOf("http://", 4) < 0 && location.indexOf("https://", 4) < 0,
        "Location header does contain multiple protocol parts");
  }

  private static String getCreatedId(HttpResponse response) {
    HttpStatus actual = response.status();
    if (actual == HttpStatus.CREATED) {
      JsonObject report = response.contentUnchecked().getObject("response");
      if (report.exists()) {
        return report.getString("uid").string();
      }
    }
    String location = response.location();
    return location == null ? null : location.substring(location.lastIndexOf('/') + 1);
  }

  /**
   * Assumes the {@link JsonError} has a {@link JsonTypeReport} containing a single {@link
   * JsonErrorReport} with the expected properties.
   *
   * @param expectedCode The code the single error is expected to have
   * @param expectedMessage The message the single error is expected to have
   * @param actual the actual error from the {@link HttpResponse}
   */
  public static void assertError(ErrorCode expectedCode, String expectedMessage, JsonError actual) {
    JsonList<JsonObjectReport> reports = actual.getTypeReport().getObjectReports();
    assertEquals(1, reports.size());
    JsonList<JsonErrorReport> errors = reports.get(0).getErrorReports();
    assertEquals(1, errors.size());
    JsonErrorReport error = errors.get(0);
    assertEquals(expectedCode, error.getErrorCode());
    assertEquals(expectedMessage, error.getMessage());
  }

  public static String substitutePlaceholders(String url, Object[] args) {
    if (args.length == 0) {
      return url;
    }
    Object[] urlArgs =
        Arrays.stream(args)
            .filter(arg -> !(arg instanceof RequestComponent))
            .map(arg -> arg == null ? "" : arg)
            .toArray();
    return String.format(url.replaceAll("\\{[a-zA-Z]+}", "%s"), urlArgs);
  }

  public static String objectReferences(String... uids) {
    StringBuilder str = new StringBuilder();
    str.append('[');
    for (String uid : uids) {
      if (str.length() > 1) {
        str.append(',');
      }
      str.append(objectReference(uid));
    }
    str.append(']');
    return str.toString();
  }

  public static String objectReference(String uid) {
    return String.format("{\"id\":\"%s\"}", uid);
  }

  public static <T> T failOnException(Callable<T> op) {
    try {
      return op.call();
    } catch (Exception ex) {
      throw new AssertionError(ex);
    }
  }

  public static RequestComponent[] requestComponentsIn(Object... args) {
    List<RequestComponent> components = new ArrayList<>();
    for (Object arg : args) {
      if (arg instanceof RequestComponent) {
        components.add((RequestComponent) arg);
      }
    }
    return components.toArray(new RequestComponent[0]);
  }

  @SuppressWarnings("unchecked")
  public static <T extends RequestComponent> T getComponent(
      Class<T> type, RequestComponent[] components) {
    for (RequestComponent c : components) {
      if (c.getClass() == type) {
        return (T) c;
      }
    }
    return null;
  }

  public static String fileContent(String filename) {
    try {
      return Files.readString(
          Path.of(
              Objects.requireNonNull(WebClientUtils.class.getClassLoader().getResource(filename))
                  .toURI()),
          StandardCharsets.UTF_8);
    } catch (IOException | URISyntaxException e) {
      fail(e);
      return null;
    }
  }

  private WebClientUtils() {
    throw new UnsupportedOperationException("util");
  }
}
